package com.bjtang.book.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjtang.book.dto.IdDto;
import com.bjtang.book.dto.ReturnDto;
import com.bjtang.book.entiy.AttentionTable;
import com.bjtang.book.myexception.DataCheckException;
import com.bjtang.book.service.AttentionService;
import com.bjtang.book.service.MyService;
import com.bjtang.book.service.SysUserDtoService;
import com.bjtang.book.util.ReturnUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "用户关注综合管理")
@Validated
@RestController
@RequestMapping(value = "/attention/")
public class AttentionController {
	@Autowired
	AttentionService attentionService;
	@Autowired
	SysUserDtoService sysUserDtoService;
	@Autowired
	private MyService myService;

	@ApiOperation("新建关注")
	@PostMapping("/")
	public ReturnDto addShare(@RequestBody @Valid AttentionTable attention) {
		List<AttentionTable> list = attentionService.lambdaQuery()
				.eq(AttentionTable::getFounder, myService.getMyselfId())
				.eq(AttentionTable::getUserNumber, attention.getUserNumber()).list();
		if (list.size() > 0)
			throw new DataCheckException("你已经关注过了");
		// 注入创建人
		attention.setFounder(myService.getMyselfId());
		return ReturnUtil.success(attentionService.save(attention));
	}

	@ApiOperation("删除指定关注")
	@DeleteMapping("/")
	public ReturnDto delArticle(@RequestBody @Valid IdDto attentionId) throws Exception {
		AttentionTable byId = attentionService.getById(attentionId.getId());
		if (byId == null)
			throw new DataCheckException("关注记录没找到");
		if (byId.getFounder().longValue() != myService.getMyselfId().longValue())
			throw new DataCheckException("没有操作权限");
		return ReturnUtil.successOrError(attentionService.removeById(attentionId.getId()), attentionId.getId());
	}

	@ApiOperation("查询某人的关注列表ByUserPage")
	@GetMapping("/{userId}/{current}/{size}")
	// 这是深夜头脑发昏想出来的实现,由于page是list,而我的dto不是,所以前端要取第一个值
	public ReturnDto GetArticleByUser(@PathVariable Long userId, @PathVariable Long current, @PathVariable Long size) {
		Page<AttentionTable> page = new Page<AttentionTable>(current, size);
		return ReturnUtil.success(attentionService.lambdaQuery().eq(AttentionTable::getFounder, userId).page(page));
	}

	@ApiOperation("查询被关注ByUserPage")
	@GetMapping("/be-concerned/{userId}/{current}/{size}")
	public ReturnDto GetArticleByUserList(@PathVariable Long userId, @PathVariable Long current,
			@PathVariable Long size) {
		Page<AttentionTable> page = new Page<AttentionTable>(current, size);
		return ReturnUtil.success(attentionService.lambdaQuery().eq(AttentionTable::getUserNumber, userId).page(page));
	}
}
